﻿package lobby
{
	import lobby.common.Utility;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import lobby.events.LobbyEvent;
	import lobby.exchangeddata.HashTable;
	import lobby.exchangeddata.HistoryInfoBase;
	import lobby.exchangeddata.HistoryInfoDragonTiger;
	import lobby.interfaces.ILanguage;
	import lobby.constants.ConstData;
	import lobby.component.combobox.*;
	
	/**
	 * ...
	 * @author Bamboo
	 */
	public class FormMain extends MovieClip implements ILanguage
	{
		private var mc:MovieClip;
		public var betStatusPanel:BetStatusPanel;
		public var gameHistoryPanel:GameHistoryPanel;
		public var gameRulePanel:GameRulePanel;
		public var hotLinePanel:HotLinePanel;
		public var accountInfoPanel:AccountInfoPanel;
		public var menuMain:GameMenuPanel;
		public var transactionReportPanel:TransactionReportPanel;
		public var welcomeMsg:WelcomeMessagePanel;
		public var tablesPanel:TablesPanel;
		public var left_menu:LeftMenuPanel;
		public var smallLogo:MovieClip;
		public var loadLogo_mc:LoadMc;
		
		public var btnLogOut:MovieClip;
		public var groupIconPanel:GroupIconPanel;
		public var langCboBox:PannelCombobox;
		public var user_txt:TextField;
		public var balance_txt:TextField;
		public var txtMember:TextField;
		public var txtBalance:TextField;
		public var currencyIcon_mc:CurrencyIconPanel;
		public var transferReportPanel:TransferReportPanel;
		
		public function FormMain()
		{
			this.visible = false;
			mc = new MovieClip();
			loadLogo_mc = new LoadMc();
			loadLogo_mc.addEventListener(LobbyEvent.MOVIE_LOADED, setLocation);
			btnLogOut.buttonMode = true;
			btnLogOut.txtTf.mouseEnabled = false;
			btnLogOut.addEventListener(MouseEvent.CLICK, hldFormMain);
			initLangCombo();
			// remove tab order
			langCboBox.tabChildren = false;
			btnLogOut.tabEnabled = false;
			btnLogOut.tabChildren = false;
			menuMain.tabChildren = false;
		}
		
		private function hldFormMain(e:MouseEvent):void
		{
			switch (e.currentTarget)
			{
				case btnLogOut: 
					dispatchEvent(new LobbyEvent(LobbyEvent.BACK_FORM_LOGIN, true));
					break;
			}
		}
		
		public function changeTextLang(info:HashTable):void
		{
			txtMember.text = GlobalVariables.userName;
			user_txt.text = info.lookupString("MNwelcome");
			balance_txt.text = info.lookupString("MNbalance");
			btnLogOut.txtTf.text = info.lookupString("MNlogout");
			groupIconPanel.initText(info);
			menuMain.initText();
			menuMain.menuAll_mc.initTextName();
			tablesPanel.changeTextLang(GlobalVariables.langInfo.hashTable);
			dispatchEvent(new LobbyEvent(LobbyEvent.UPDATE_WELCOME_MASSAGE, true));
			left_menu.changeTextLang();
		}
		
		public function updateUserInfo(result:String):void
		{
			txtBalance.text = Utility.currency(Number(result.substr(ConstData.POS_MEMBERBETINFO_CREDIT, ConstData.LEN_MEMBERBETINFO_CREDIT)), 2, "");
		}
		
		private function setLocation(e:LobbyEvent):void
		{
			var ratio = Math.min(smallLogo.width / mc.width, smallLogo.height / mc.height);
			mc.scaleX = mc.scaleY = ratio;
			smallLogo.addChild(mc);
			mc.x = (smallLogo.width - mc.width) / 2;
			mc.y = (smallLogo.height - mc.height) / 2;
		}
		
		/**
		 * @author	Tom
		 */
		public function loadLogo():void
		{
			var logo_linked:String = GlobalVariables.subDomain;
			if (logo_linked == "localhost")
				logo_linked = "all";
			var swfName:String = "logo/" + logo_linked + "/logo_s.swf";
			loadLogo_mc.loadSwf(swfName, mc);
		}
		
		/**
		 * @author	Tom
		 */
		public function showHotlinePanel():void
		{
			hotLinePanel.addText();
			hotLinePanel.initText(GlobalVariables.langInfo.hashTable);
			hotLinePanel.show();
			setChildIndex(hotLinePanel, this.numChildren - 1);
		}
		
		/**
		 * @author	Tom
		 */
		public function showGameRulePanel():void
		{
			gameRulePanel.initText(GlobalVariables.langInfo.hashTable);
			gameRulePanel.loadTxt("ruleGames.txt");
			gameRulePanel.show();
			setChildIndex(gameRulePanel, this.numChildren - 1);
		}
		
		/**
		 * @author	Tom
		 * @param	dateFrom
		 * @param	dateTo
		 */
		public function updateDateHistory(dateFrom:String, dateTo:String):void
		{
			gameHistoryPanel.updateDate(dateFrom, dateTo);
		}
		
		/**
		 * @author	Tom
		 * @param	position
		 * @return
		 */
		public function sendDateHistoryInfo(position:int):String
		{
			if (position == 1)
			{
				return gameHistoryPanel.txtDateFromTi.text;
			}
			else
			{
				return gameHistoryPanel.txtDateToTi.text;
			}
		}
		
		public function sendDateTransactionInfo(position:int):String
		{
			if (position == 1)
			{
				return this.transactionReportPanel.txtDateFromTi.text;
			}
			else
			{
				return this.transactionReportPanel.txtDateToTi.text;
			}
		}
		
		public function sendDateTransferInfo(position:int):String
		{
			if (position == 1)
			{
				return this.transferReportPanel.txtDateFromTi.text;
			}
			else
			{
				return this.transferReportPanel.txtDateToTi.text;
			}
		}
		
		/**
		 * @author	Tom
		 */
		public function showGameHistoryPanel():void
		{
			gameHistoryPanel.initText(GlobalVariables.langInfo.hashTable);
			gameHistoryPanel.show();
			setChildIndex(gameHistoryPanel, this.numChildren - 1);
		}
		
		/**
		 * @author	Tom
		 */
		public function updateGameHistory(info:Object):void
		{
			gameHistoryPanel.updateValueHistory(info);
		}
		
		/**
		 * @author	Tom
		 */
		public function updateBetStatusValue(info:Object):void
		{
			betStatusPanel.updateValueBetStatus(info);
		}
		
		/**
		 * @author	Tom
		 */
		public function showBetStatusPanel():void
		{
			if (betStatusPanel.visible == false)
			{
				betStatusPanel.initText(GlobalVariables.langInfo.hashTable);
				betStatusPanel.show();
				setChildIndex(betStatusPanel, this.numChildren - 1);
			}
		}
		
		public function updateAccountInfoValue(info:Object):void
		{
			this.accountInfoPanel.updateValueAccountInfo(info);
		}
		
		public function showAccountInfoPanel():void
		{
			this.accountInfoPanel.show();
			this.accountInfoPanel.initText(GlobalVariables.langInfo.hashTable);
			setChildIndex(accountInfoPanel, this.numChildren - 1);
		}
		
		public function transactionUpdateDateReport(dateFrom:String, dateTo:String):void
		{
			this.transactionReportPanel.updateDate(dateFrom, dateTo);
		}
		
		public function transferUpdateDateReport(dateFrom:String, dateTo:String):void
		{
			trace("dateFrom : " + dateFrom + " : " + dateTo);
			this.transferReportPanel.updateDate(dateFrom, dateTo);
		}
		
		public function updateValueTransactionReport(info:Object):void
		{
			this.transactionReportPanel.updateValueTransactionReport(info);
		}
		
		public function updateValueTransferReport(info:Object):void
		{
			this.transferReportPanel.updateValueTransferReport(info);
		}
		
		public function updateValueDetailTransactionReport(info:Object):void
		{
			this.transactionReportPanel.updateValueDetailTransactionReport(info);
		}
		
		public function showTransactionReport():void
		{
			//hien report
			this.transactionReportPanel.show();
			this.transactionReportPanel.initText(GlobalVariables.langInfo.hashTable);
			setChildIndex(transactionReportPanel, this.numChildren - 1);
		}
		
		public function showTransferReport():void
		{
			this.transferReportPanel.show();
			this.transferReportPanel.initText(GlobalVariables.langInfo.hashTable);
			//hien report
		}
		
		/**
		 * @author	Tom
		 * @param	tablesList
		 */
		public function loadTables(tablesList:Array):void
		{
			tablesPanel.loadTables(tablesList);
		}
		
		/**
		 * @author	Tom
		 * @param	tbleId
		 * @param	dataHis
		 */
		public function updateTableHistory(tableId:String, hisData:Array):void
		{
			tablesPanel.updateTableHistory(tableId, hisData);
		}
		
		public function updateWelcomeMessage(strMsg:String):void
		{
			trace("updateWelcomeMessage", strMsg)
			this.welcomeMsg.startWelcome(strMsg);
		}
		
		public function updateListCasino(_arr:Array)
		{
			left_menu.initMenuLeft(_arr);
		}
		
		public function getListGameCasino(_arr:Array)
		{
			left_menu.getListGameCasino(_arr);
		}
		
		/**
		 * @author	Tom
		 */
		public function updateComboBox():void
		{
			langCboBox.setDefaultItem(GlobalVariables.lang);
		}
		
		/**
		 * @author	Tom
		 */
		private function initLangCombo():void
		{
			var dataCombobox:Array = new Array();
			var obj:Object = new Object();
			obj["name"] = "G";
			obj["value"] = "简体中文"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "B";
			obj["value"] = "繁體中文"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "T";
			obj["value"] = "ภาษาไทยv"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "K";
			obj["value"] = "한국어"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "J";
			obj["value"] = "日本語"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "V";
			obj["value"] = "Tiếng Việt"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "M";
			obj["value"] = "Indonesia"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "E";
			obj["value"] = "English"
			dataCombobox.push(obj);
			langCboBox.addEventListener(EventCombobox.COMPLETED_INIT_ITEM, hdlInitCompleted);
			langCboBox.initCombobox(dataCombobox, "name", "value");
			langCboBox.addEventListener(EventCombobox.CHANGED, hdlSelect);
		}
		
		/**
		 * @author	Tom
		 * @param	e
		 */
		private function hdlInitCompleted(e:EventCombobox):void
		{
			langCboBox.setDefaultItem(GlobalVariables.lang);
		}
		
		/**
		 * @author	Tom
		 * @param	e
		 */
		private function hdlSelect(e:EventCombobox):void
		{
			GlobalVariables.lang = e.currentName;
			GlobalVariables.langInfo.updateLangInfo(GlobalVariables.lang); //change and update lang
		}
		
		public function getCurrentIcon(icon:String):void
		{
			currencyIcon_mc.setCurrencyIncon(icon);
		}
		
		public function deleteTableHistory(tableId:String):void
		{
			tablesPanel.deleteTableHistory(tableId);
		}
	}
}